<template>
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { reqAddEmployeeBatch } from '@/api/employees'
export default {
  name: 'Import',
  methods: {
    handleSuccess({ header, results }) {
      console.log(header, results)
      if (this.$route.query.type === 'user') {
        this.handleEmployeeBatch(header, results)
      }
      if (this.$route.query.type === 'money') {
        console.log('工资的批量导入')
      }
      if (this.$route.query.type === 'social') {
        console.log('社保的批量导入')
      }
    },
    async handleEmployeeBatch(header, results) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const arr = []
      results.forEach(item => {
        const obj = {}
        for (const key in item) {
          const englishKey = userRelations[key]
          if (englishKey === 'timeOfEntry' || englishKey === 'correctionTime') {
            obj[englishKey] = this.formatExcelDate(item[key], '-')
          } else {
            obj[englishKey] = item[key]
          }
        }

        arr.push(obj)
      })
      console.log(arr)
      const res = await reqAddEmployeeBatch(arr)
      console.log(res)
      this.$message.success('批量导入员工成功')
      this.$router.push('/employees')
    },
    formatExcelDate(numb, format) {
      const time = new Date(numb * 24 * 3600000 + 1) // 毫秒
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date)
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }
}
</script>

<style>

</style>
